//
//  ThirdViewController.swift
//  学唐诗
//
//  Created by 申克 on 16/5/10.
//  Copyright © 2016年 shang_hongzhi. All rights reserved.
//

import UIKit
import AVFoundation

class ThirdViewController: UIViewController {
    
    var poetryTitle :String!
    var poetryName : String!
    var poetryStr :String!
    var audio :AVAudioPlayer!
    
    
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var firstLabel: UILabel!
    @IBOutlet weak var secondLabel: UILabel!
    @IBOutlet weak var thirdLabel: UILabel!
    @IBOutlet weak var fouthLabel: UILabel!
    @IBOutlet weak var repeatButton: UIButton!
    @IBOutlet weak var nextButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        titleLabel.text = poetryTitle
        nameLabel.text = poetryName
        let str = poetryStr as NSString
        let lenth = str.length
        if lenth > 36 {
            firstLabel.text = str.substringWithRange(NSMakeRange(0, lenth/4))
            secondLabel.text = str.substringWithRange(NSMakeRange(lenth/4, lenth/4))
            thirdLabel.text = str.substringWithRange(NSMakeRange(lenth/2, lenth/4))
            fouthLabel.text = str.substringWithRange(NSMakeRange(lenth * 3 / 4, lenth/4))
        }else{
            firstLabel.text = str.substringWithRange(NSMakeRange(0, lenth/3))
            secondLabel.text = str.substringWithRange(NSMakeRange(lenth/3, lenth/3))
            thirdLabel.text = str.substringWithRange(NSMakeRange(lenth * 2/3, lenth/3))
            fouthLabel.text = ""
        }
        let workingQueue = dispatch_queue_create("my_queue", nil)
        // 派发到刚创建的队列中，GCD 会负责进行线程调度
        dispatch_async(workingQueue) {
            NSThread.sleepForTimeInterval(1.5)  // 模拟两秒的执行时间
            dispatch_async(dispatch_get_main_queue()) {
                self.playWithAudioName(self.poetryTitle)
            }
        }
        
        let button = UIButton.init(type: UIButtonType.Custom)
        button.frame = CGRectMake(10, 0, 60, 40)
        button.setTitle("返回", forState: UIControlState.Normal)
        button.backgroundColor = UIColor.clearColor()
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        self.view.addSubview(button)
        button.addTarget(self, action: #selector(SecondViewController.returnButtonClick), forControlEvents: UIControlEvents.TouchUpInside)

    }
    
    func returnButtonClick(){
        self.navigationController?.popViewControllerAnimated(true)
        
    }
     func playWithAudioName(audioName : String){
        
        let path = NSBundle.mainBundle().pathForResource(audioName, ofType: "mp3")
        let playerURL = NSURL.init(fileURLWithPath: path!)
        do{
            try audio = AVAudioPlayer(contentsOfURL: playerURL)
            audio .play()
        }catch let error as NSError{
            print(error.description)
        }
        
    }
    

//    @IBAction func repeatReadButtonClick(sender: AnyObject) {
//        audio.play()
//        
//    }
    @IBAction func nextButtonClick(sender: AnyObject) {
        audio.play()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
